package com.spacex.danhuang.polive.activities;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.spacex.danhuang.polive.Application.VApplication;
import com.spacex.danhuang.polive.Constants.Constants;
import com.spacex.danhuang.polive.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import io.socket.client.Socket;
import io.socket.emitter.Emitter;

public class ChatRoomActivity extends BaseActivity {
    private static final String TAG = "ChatRoomActivity";

    private VApplication app;
    private Socket mSocket;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_room);

        app = (VApplication)getApplication();
        mSocket = app.getSocket();
    }

    @Override
    protected void onResume() {
        super.onResume();

        mSocket.on("message", onHandleMsg);
    }

    @Override
    protected void onPause() {
        super.onPause();

        mSocket.off("message", onHandleMsg);
    }

    protected void attemptSendMsg(String msg) {
        if (!mSocket.connected()) {
            return;
        }

        JSONObject tmpSendMsgJSON = new JSONObject();
        JSONObject tmpDataJSON = new JSONObject();
        try {
            tmpSendMsgJSON.put("request", Constants.SEND_MSG_NAME);

            tmpDataJSON.put("msgtype", 1);
            tmpDataJSON.put("msgbody", msg);
            tmpSendMsgJSON.put("data", tmpDataJSON);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String tmpSendMsgStr = tmpSendMsgJSON.toString();
        Log.i(TAG, "send msg");
        Log.i(TAG, tmpSendMsgStr);
        mSocket.send(tmpSendMsgStr);
    }

    protected void attemptExitRoom() {
        JSONObject tmpExitRoomJSON = new JSONObject();
        try {
            tmpExitRoomJSON.put("request", Constants.EXIT_ROOM_NAME);
            tmpExitRoomJSON.put("data", "");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String tmpExitRoomStr = tmpExitRoomJSON.toString();
        Log.i(TAG, "exit room");
        Log.i(TAG, tmpExitRoomStr);
        mSocket.send(tmpExitRoomStr);
    }

    /**
     * Socket事件监听处理函数（收到"message"事件时触发）
     **/
    protected Emitter.Listener onHandleMsg = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            Log.i(TAG, "onHandleMsg received data");

            String onHandleMsgStr = (String)args[0];
            Log.i(TAG, onHandleMsgStr);
            JSONObject onHandleMsgJSON = null;
            try {
                onHandleMsgJSON = new JSONObject(onHandleMsgStr);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            String rsp_name = "";
            int rsp_status = 0;
            String rsp_errmsg = "";

            JSONObject onHandleMsgData = null;

            try {
                if (onHandleMsgJSON != null) {
                    rsp_name = onHandleMsgJSON.getString("response");
                    rsp_status = onHandleMsgJSON.getInt("status");
                    rsp_errmsg = onHandleMsgJSON.getString("errormsg");
                    Log.i(TAG, String.format("activity rsp_name = %s", rsp_name));
                    Log.i(TAG, String.format("rsp_status = %d", rsp_status));
                    Log.i(TAG, String.format("rsp_errmsg = %s", rsp_errmsg));

                    onHandleMsgData = onHandleMsgJSON.getJSONObject("data");
                }

            } catch (JSONException e) {
                e.printStackTrace();
                return;
            }

            handleSwitchMethod(rsp_name, rsp_status, rsp_errmsg, onHandleMsgData);
        }
    };

    protected void handleSwitchMethod(String rsp_name, int rsp_status, String rsp_errmsg, JSONObject onHandleMsgData) {

    }


}
